package projectManagementServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class AddUserGUI extends JPanel implements ActionListener{
	private JTextField userNameField;
	private JPasswordField passwordField;
	
	JFrame f;
	private JButton btnCancel;
	public AddUserGUI() {
		setPreferredSize(new Dimension(365, 203));
		setLayout(null);
		
		JLabel lblUserName = new JLabel("UserName");
		lblUserName.setHorizontalAlignment(SwingConstants.CENTER);
		lblUserName.setBounds(142, 37, 88, 14);
		add(lblUserName);
		
		userNameField = new JTextField();
		userNameField.setBounds(102, 49, 161, 40);
		add(userNameField);
		userNameField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
		lblPassword.setBounds(142, 100, 84, 14);
		add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(102, 113, 161, 40);
		add(passwordField);
		
		JButton btnOk = new JButton("OK");
		btnOk.setBounds(101, 158, 83, 34);
		btnOk.putClientProperty("Quaqua.Button.style", "bevel");
		btnOk.addActionListener(this);
		add(btnOk);
		
		btnCancel = new JButton("CANCEL");
		btnCancel.setBounds(180, 158, 85, 34);
		btnCancel.putClientProperty("Quaqua.Button.style", "bevel");
		btnCancel.addActionListener(this);
		add(btnCancel);
		
		f = new JFrame();
		f.getContentPane().add(this);
		f.setResizable(false);
		f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		f.setVisible(true);
		f.pack();
		f.setLocationRelativeTo(null);
	}
	@Override
	public void actionPerformed(ActionEvent btn) {
		String cmd = btn.getActionCommand();
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		
		if(cmd.equalsIgnoreCase("OK")){
			Member mem = new Member(userNameField.getText(), String.valueOf(passwordField.getPassword()), "ACTIVE", (dateFormat.format(cal.getTime()))+"");
			AccessDatabase dataBase = new AccessDatabase();
			
			try {
				dataBase.addMember(mem);
				ServerGUI.addUser(mem);
			} catch (Exception e) {e.printStackTrace();}
			
			
			f.setVisible(false);
		}
		
		if(cmd.equalsIgnoreCase("Cancel")){
			f.setVisible(false);
		}
		
	}
}
